package cn.kgc.tangcco.context;

import cn.kgc.tangcco.utils.date.BaseDateUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Enumeration;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/10/15 下午5:15
 */
@WebServlet(urlPatterns = "/context03")
public class ServletContext03 extends HttpServlet {
    private static final long serialVersionUID = -4483250561115553026L;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取ServletContext对象
        ServletContext context = this.getServletContext();
        System.out.println(request.getContextPath());
        System.out.println(context.getContextPath());
        System.out.println(this.getServletContext().getContextPath());

        // 获取web项目的系统绝对路径
        System.out.println(this.getServletContext().getRealPath("/"));
        String realPath = this.getServletContext().getRealPath("/");
        String uploadPath = realPath + File.separator + "upload" + File.separator + BaseDateUtil.format(new Date(),"yyyy/MM/dd/HH");
        File file = new File(uploadPath);
        if (!file.exists()){
            file.mkdirs();
        }
        System.out.println(this.getServletContext().getRealPath("/upload"));
    }
}
